home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 011 / arc43.lbr / ARCCVT.MQC / arccvt.mac
Text File  |  1985-10-30  |  5KB  |  105 lines

  1. /*  ARC - Archive utility - ARCCVT
  2.  
  3. $define(tag,$$segment(@1,$$index(@1,=)+1))#
  4. $define(version,Version $tag(
  5. TED_VERSION DB =1.06), created on $tag(
  6. TED_DATE DB =08/22/85) at $tag(
  7. TED_TIME DB =12:22:33))#
  8. $undefine(tag)#
  9.     $version
  10.  
  11. (C) COPYRIGHT 1985 by System Enhancement Associates; ALL RIGHTS RESERVED
  12.  
  13.     By:  Thom Henderson
  14.  
  15.     Description:
  16.          This file contains the routines used to convert archives to use
  17.          newer file storage methods.
  18.  
  19.     Language:
  20.          Computer Innovations Optimizing C86
  21. */
  22. #include <stdio.h>
  23. #include "arc.h"
  24.  
  25. static char tempname[$strlen];         /* temp file name */
  26.  
  27. cvtarc(num,arg)                        /* convert archive */
  28. int num;                               /* number of arguments */
  29. char *arg[];                           /* pointers to arguments */
  30. {
  31.     struct heads hdr;                  /* file header */
  32.     int cvt;                           /* true to convert current file */
  33.     int did[$maxarg];                  /* true when argument was used */
  34.     int n;                             /* index */
  35.     char *makefnam();                  /* filename fixer */
  36.     FILE *fopen();                     /* file opener */
  37.  
  38.     if(fopen(makefnam(".TMP",arcname,tempname),"r"))
  39.          abort("Temp file %s already exists",tempname);
  40.  
  41.     openarc(1);                        /* open archive for changes */
  42.  
  43.     for(n=0; n<num; n++)               /* for each argument */
  44.          did[n] = 0;                   /* reset usage flag */
  45.  
  46.     if(num)                            /* if files were named */
  47.     {    while(readhdr(&hdr,bak))      /* while more files to check */
  48.          {    cvt = 0;                 /* reset convert flag */
  49.               for(n=0; n<num; n++)     /* for each template given */
  50.               {    if(match(hdr.name,arg[n]))
  51.                    {    cvt = 1;       /* turn on convert flag */
  52.                         did[n] = 1;    /* turn on usage flag */
  53.                         break;         /* stop looking */
  54.                    }
  55.               }
  56.  
  57.               if(cvt)                  /* if converting this one */
  58.                    cvtfile(&hdr);      /* then do it */
  59.               else                     /* else just copy it */
  60.               {    writehdr(&hdr,arc);
  61.                    filecopy(bak,arc,hdr.size);
  62.               }
  63.          }
  64.     }
  65.  
  66.     else while(readhdr(&hdr,bak))      /* else convert all files */
  67.          cvtfile(&hdr);
  68.  
  69.     hdrver = 0;                        /* archive EOF type */
  70.     writehdr(&hdr,arc);                /* write out our end marker */
  71.     closearc(1);                       /* close archive after changes */
  72.  
  73.     if(note)
  74.          for(n=0; n<num; n++)          /* report unused args */
  75.               if(!did[n])
  76.                    printf("File not found: %s\n",arg[n]);
  77. }
  78.  
  79. static cvtfile(hdr)                    /* convert a file */
  80. struct heads *hdr;                     /* pointer to header data */
  81. {
  82.     long starts, ftell();              /* where the file goes */
  83.     FILE *tmp, *fopen();               /* temporary file */
  84.  
  85.     if(!(tmp=fopen(tempname,"wrb")))
  86.          abort("Unable to create temporary file %s",tempname);
  87.  
  88.     if(note)
  89.          printf("Converting file: %s   reading,",hdr->name);
  90.  
  91.     unpack(bak,tmp,hdr);               /* unpack the entry */
  92.     fseek(tmp,0L,0);                   /* reset temp for reading */
  93.  
  94.     starts = ftell(arc);               /* note where header goes */
  95.     hdrver = $arcver;                  /* anything but end marker */
  96.     writehdr(hdr,arc);                 /* write out header skeleton */
  97.     pack(tmp,arc,hdr);                 /* pack file into archive */
  98.     fseek(arc,starts,0);               /* move back to header skeleton */
  99.     writehdr(hdr,arc);                 /* write out real header */
  100.     fseek(arc,hdr->size,1);            /* skip over data to next header */
  101.     fclose(tmp);                       /* all done with the file */
  102.     if(unlink(tempname) && warn)
  103.          printf("Cannot unsave %s\n",tempname);
  104. }
  105.